package com.tg.paw.common.config;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.tg.paw.common.component.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;

import java.util.Date;

/**
 * 自定义填充公共 name 字段
 *
 * @author Administrator
 */
@Slf4j
public class DateMetaObjectHandler implements MetaObjectHandler {


    /**
     * 插入填充，字段为空自动填充
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        log.debug("start insert fill ....");
        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        String username = UserUtil.getLoginUsername();
        this.strictInsertFill(metaObject, "creator", String.class, username);

    }

    /**
     * 更新填充
     *
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        log.debug("start update fill ....");
        this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
        String username = UserUtil.getLoginUsername();
        if(StrUtil.isNotEmpty(username)){
            this.strictUpdateFill(metaObject, "modifier", String.class, username);
        }
    }
}